package com.shuorikeji.huohuo.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.shuorikeji.huohuo.R;
import com.shuorikeji.huohuo.adapter.NewBookQsItemAdapter;
import com.shuorikeji.huohuo.base.BasePresenterActivity;
import com.shuorikeji.huohuo.bean.AllBookMainBean;
import com.shuorikeji.huohuo.bean.BookDetailInfoTempBean;
import com.shuorikeji.huohuo.callback.ClickCallBack;
import com.shuorikeji.huohuo.mvp.AllPresenter;
import com.shuorikeji.huohuo.utils.SpacesItemDecoration;
import com.shuorikeji.huohuo.utils.ToastShowUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class BookListActivity extends BasePresenterActivity<AllPresenter> {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.srl_layout)
    SmartRefreshLayout srlLayout;
    @BindView(R.id.rv_data)
    RecyclerView rvData;

    private int curPage;
    private String TITLE;
    private List<BookDetailInfoTempBean> qlNewBookList = new ArrayList<>();//潜力新书
    private NewBookQsItemAdapter adapter;
    private int HOT_TAG_ID;
    private List<BookDetailInfoTempBean> hotTagBookList = new ArrayList<>();//热门标签书籍
    private List<BookDetailInfoTempBean> wjzxBookList = new ArrayList<>();//完结甄选书籍
    private int YJ_TYPE_ID, EJ_TYPE_ID, SJ_TYPE_ID;
    private List<BookDetailInfoTempBean> rankBookList = new ArrayList<>();//排行书籍

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getmImmersionBar().reset().statusBarColor(R.color.transparent).statusBarDarkFont(true, 0.2f).init();
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_book_list);
    }

    @Override
    protected void initData() {
        curPage = 1;
        initSmartRefreshLayout();
        initAdapter();
        initIntent();
        ivBack.setOnClickListener(view -> {
            finish();
        });
    }

    private void initAdapter() {
        rvData.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        rvData.addItemDecoration(new SpacesItemDecoration(0, 0, 10, 0));
        adapter = new NewBookQsItemAdapter(mContext, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        BookDetailInfoTempBean data = (BookDetailInfoTempBean) object;
                        Intent intent = new Intent(BookListActivity.this, BookInfoActivity.class);
                        intent.putExtra("BOOK_ID", data.getId());
                        startActivity(intent);
                        break;
                }
            }
        });
        rvData.setAdapter(adapter);
    }

    private void initSmartRefreshLayout() {
        srlLayout.setRefreshHeader(new ClassicsHeader(BookListActivity.this));
        srlLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                //刷新
                curPage = 1;
                if ("书籍".equals(TITLE)) {
                    getAllBook(false, 14, -1, -1, 1, -1, 2, -1, 0, YJ_TYPE_ID, EJ_TYPE_ID, SJ_TYPE_ID);
                }
            }
        });
        srlLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                //加载更多
                curPage = curPage + 1;
                if ("书籍".equals(TITLE)) {
                    getAllBook(false, 14, -1, -1, 1, -1, 2, -1, 0, YJ_TYPE_ID, EJ_TYPE_ID, SJ_TYPE_ID);
                }
            }
        });
    }

    private void initIntent() {
        Intent intent = getIntent();
        TITLE = intent.getStringExtra("TITLE");
        HOT_TAG_ID = intent.getIntExtra("HOT_TAG_ID", -1);
        YJ_TYPE_ID = intent.getIntExtra("YJ_TYPE_ID", -1);
        EJ_TYPE_ID = intent.getIntExtra("EJ_TYPE_ID", -1);
        SJ_TYPE_ID = intent.getIntExtra("SJ_TYPE_ID", -1);
        tvTitle.setText(TITLE);
        if ("书籍".equals(TITLE)) {
            getAllBook(false, 14, -1, -1, 1, -1, 2, -1, 0, YJ_TYPE_ID, EJ_TYPE_ID, SJ_TYPE_ID);
        }
    }

    /**
     * 获取所有书籍
     *
     * @param flagTemp 1新书严选
     *                 2潜力新书
     *                 3新书首发
     *                 4精选
     *                 5banner
     *                 6完结甄选
     *                 7热门标签书籍
     *                 8会员书库
     *                 9大家都在看
     */
    private void getAllBook(boolean show, int flagTemp, int sort, int score, int winnow, int banner, int completed, int tagId,
                            int is_vip, int yjTypeId, int ejTypeId, int sjTypeId) {
        mPresenter.getAllBook(BookListActivity.this, flagTemp, sort, 20, curPage, "", yjTypeId,
                ejTypeId, -1, tagId, completed, is_vip, -1, score,
                winnow, banner, "", sjTypeId, show, new ClickCallBack() {
                    @Override
                    public void onClick(int flag, Object object) {
                        srlLayout.finishRefresh();
                        srlLayout.finishLoadMore();
                        switch (flag) {
                            case 1:
                                AllBookMainBean data = (AllBookMainBean) object;
                                if (flagTemp == 14) {
                                    if (curPage == 1) {
                                        rankBookList.clear();
                                    }
                                    rankBookList.addAll(data.getData().getData());
                                    adapter.setData(rankBookList);
                                }
                                break;
                            case 2:
                                String msg = (String) object;
                                ToastShowUtil.showToastCenter(BookListActivity.this, msg);
                        }
                    }
                });
    }
}